package com.guit.hrms.controller;

import com.guit.hrms.po.Setting;
import com.guit.hrms.po.utilsPO.Result;
import com.guit.hrms.service.SettingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/settings")
public class SettingsController {
    @Autowired
    private SettingsService settingsService;

    /**
     * 获取系统设置
     * @return 设置列表
     */
    @GetMapping
    public Result getSettings(){
        List<Setting> settings = settingsService.getSettings();
        return Result.success(settings);
    }

    /**
     * 更新系统设置
     * @param settingId 设置ID
     * @param settingValue 设置值
     * @return 更新结果
     */
    @PutMapping
    public Result updateSetting(@RequestParam Integer settingId, 
                              @RequestParam(required = false) String settingValue) {
        try {
            boolean success = settingsService.updateSetting(settingId, settingValue);
            if (success) {
                return Result.success();
            } else {
                return Result.error("更新失败，请检查设置ID是否存在");
            }
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统设置更新失败：" + e.getMessage());
        }
    }
}
